package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._

object Demo1WordCount {
  def main(args: Array[String]): Unit = {


    /**
     * 1、创建flink环境
     *
     */
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置flink任务的并行度
    //默认和电脑的核数有关
    env.setParallelism(2)


    //数据从上游发送到下游的超时时间
    //默认是200毫秒
    env.setBufferTimeout(200)


    /**
     * 2、读取数据
     *
     */
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    /**
     * 3、统计单词的数量
     *
     */
    //将一行转换成多行
    val wordsDS: DataStream[String] = linesDS.flatMap((line: String) => line.split(","))

    //转换成kv格式
    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))

    //按照单词进行分组
    val groupByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(kv => kv._1)

    //d对value进行汇总
    val countDS: DataStream[(String, Int)] = groupByDS.sum(1)

    /**
     * 4、查看结果
     *
     */
    countDS.print()


    /**
     * 启动flink程序
     *
     */
    env.execute()

  }

}
